<template>
	<main-layout :breadcrumbs="[{ name: '模板页面', to: '' }]">
		<template slot="formInline">
			<ButtonGroup style="margin-right:24px;"><Button title="新增" icon="md-add" @click="editModal = true"></Button></ButtonGroup>
			<FormItem><Input type="text" placeholder="请输入测试内容" clearable v-model="pageRequest.condition.test" /></FormItem>
			<FormItem><Button type="primary" icon="ios-search" shape="circle" title="查询" @click="list"></Button></FormItem>
		</template>
		<template slot-scope="{ height }">
			<Table :loading="loading" :columns="tableColumns" :data="pageData.content" :height="height" @on-selection-change="onSelectionChange" border>
				<template slot-scope="{ row, index }" slot="action">
					<Button type="warning" size="small" style="margin-right: 5px" @click="modify(row)">修改</Button>
					<Button type="error" size="small" style="margin-right: 5px" @click="remove(row)">删除</Button>
				</template>
				<template slot="footer">
					<Page style="margin-left:10px;" :total="pageData.total" :current="pageData.page" :page-size="15" @on-change="changePage"></Page>
				</template>
			</Table>
			<!-- 编辑对话框 -->
			<Modal :width="600" v-model="editModal" @on-visible-change="onVisibleChange" title="编辑" footer-hide>
				<Form ref="form" :model="info" :label-width="100" :rules="rules">
					<FormItem label="测试内容" prop="test"><Input v-model="info.test" type="text" placeholder="请输入测试内容" /></FormItem>
					<FormItem><Button type="primary" @click="save">保存</Button></FormItem>
				</Form>
			</Modal>
		</template>
	</main-layout>
</template>

<script language="JavaScript">
export default {
	components: {},
	data() {
		return {
			loading: false, // 加载中
			tableColumns: [
				{
					type: 'selection',
					width: 80
				},
				{
					title: '测试标题',
					key: 'test',
					width: 'auto',
					minWidth: 250,
					resizable: true
				},
				{
					title: '操作',
					slot: 'action',
					fixed: 'right',
					width: 200
				}
			], // 表格列
			// 分页请求
			pageRequest: {
				page: 1,
				rows: 15,
				sort: 'id:asc',
				condition: {}
			},
			// 分页查询结果
			pageData: {
				page: 1,
				total: 2,
				content: [] // 表格数据
			},
			editModal: false, // 编辑对话框显示
			info: {
				test: ''
			}, // 保存信息
			rules: {
				test: [
					{
						required: true,
						message: '必填'
					}
				]
			}, // 验证规则
			selection: [] //已选记录
		};
	},
	computed: {},
	watch: {},
	created: function() {},
	mounted: function() {
		this.$nextTick(() => {
			// this.list()
		});
	},
	methods: {
		// 保存
		save: function() {
			this.$refs['form'].validate(valid => {
				if (valid) {
					this.$request.post(
						'',
						this.info,
						res => {
							this.editModal = false;
							this.list();
						},
						true,
						true
					);
				}
			});
		},
		// 获取列表
		list: function() {
			this.$request.post(
				'',
				this.pageRequest,
				res => {
					this.pageData = res.data || this.pageData;
				},
				false,
				false
			);
		},
		// 修改
		modify: function(row) {
			this.editModal = true;
			this.$nextTick(() => {
				this.info = Object.assign({}, row);
			});
		},
		// 删除
		remove: function(row) {
			this.$Modal.confirm({
				title: '确认？',
				content: '删除后不可恢复，是否继续？',
				onOk: () => {
					this.$request.post(
						'',
						row,
						res => {
							this.list();
						},
						true,
						true
					);
					this.selection = [];
				}
			});
		},
		// 关闭对话框
		onVisibleChange: function() {
			this.$refs['form'].resetFields();
			this.info.id = '';
		},
		// 表格多选事件
		onSelectionChange: function(sel) {
			this.selection = sel;
		},
		// 分页
		changePage: function(page) {
			this.pageRequest.page = page;
			this.list();
		}
	}
};
</script>

<style lang="scss" scoped="scoped"></style>
